<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <base href="<%=request.getContextPath()%>/">
    <title>Title</title>
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- bootstrap-table样式文件 -->
    <link rel="stylesheet" href="bootstrap-table/bootstrap-table.min.css">
    <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
    <!-- bootstrap-table的js文件 -->
    <script type="text/javascript" src="bootstrap-table/bootstrap-table.min.js"></script>
    <!-- bootstrap-table的语言包 -->
    <script type="text/javascript" src="bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
    <script type="text/javascript">
        $(function () {
            initTable();
        });
        //初始化数据表格
        function initTable() {
            $("#tab").bootstrapTable({
                height: 560,
                //远程访问数据
                url : "menu/list",
                method : "get",
                //提交的参数
                queryParams : function (params) {
                    params["menuname"] = $("#menuname").val();
                    params["type"] = $("#type").val();
                    return params;
                },
                //点击行选中复选框
                clickToSelect: true,
                //显示刷新按钮
                showRefresh: true,
                //工具栏
                toolbar : "#toolbar",
                //分页相关
                sidePagination: "server",
                queryParamsType: "",
                pagination: true,
                pageSize:10,
                pageList: [10,20,30,50],
                paginationPreText: "上一页",
                paginationNextText: "下一页",
                //表格中的列描述
                columns: [
                    {
                        checkbox: true
                    },{
                        field: "menuid",
                        visible: false
                    },{
                        field: "menuname",
                        title: "菜单名称",
                        halign: "center"
                    },{
                        field: "sort",
                        title: "显示顺序",
                        halign: "center"
                    },{
                        field: "url",
                        title: "请求地址",
                        halign: "center"
                    },{
                        field: "type",
                        title: "菜单类型",
                        halign: "center",
                        formatter: function (value) {
                            return value=="M"?"目录":(value=="C"?"菜单":"按钮");
                        }
                    },{
                        field: "icon",
                        title: "菜单图标",
                        halign: "center"
                    },{
                        field: "parent",
                        title: "父菜单名称",
                        halign: "center",
                        formatter: function (value) {
                            return value.menuname;
                        }
                    },{
                        field: "parent",
                        title: "父菜单类型",
                        halign: "center",
                        formatter: function (value) {
                            return value.type=="M"?"目录":(value.type=="C"?"菜单":"按钮");
                        }
                    }
                ]
            });
        }

        //打开新增窗体
        function preInsert() {
            $.ajax({
                url : "menu/list/all",
                type : "get",
                success: function (res) {
                    if(res.status==200){
                        var str = "<option value='0'>没有父菜单</option>";
                        $.each(res.data, function () {
                            str += "<option value='" + this.menuid + "'>" + this.menuname + "</option>";
                        });
                        $("#in_parent").html(str);
                        $("#insert_win").modal("show");
                    }
                }
            });
        }
        //新增
        function doInsert() {
            $.ajax({
                url : "menu/add",
                type : "post",
                data : {
                    menuname : $("#in_menuname").val(),
                    sort : $("#in_sort").val(),
                    url : $("#in_url").val(),
                    type : $("#in_type").val(),
                    icon : $("#in_icon").val(),
                    "parent.menuid" : $("#in_parent").val(),
                    remark : $("#in_remark").val(),
                    creator : $("#in_creator").val()
                },
                success: function (res) {
                    if(res.status==200){
                        alert("新增成功");
                        $("#insert_win").modal("hide");
                        $("#tab").bootstrapTable("refresh");
                        $("#insert_form")[0].reset();
                    }
                }
            });
        }
        //打开修改窗体
        function preUpdate() {
            var rows = $("#tab").bootstrapTable("getSelections");
            if(rows.length==0){
                alert("请选择一条要修改的数据");
            }else if(rows.length>1){
                alert("只能修改一条数据");
            }else{
                $.ajax({
                    url : "menu/get/"+rows[0].menuid,
                    type : "get",
                    success : function (res) {
                        $("#up_menuid").val(res.data.menuid);
                        $("#up_menuname").val(res.data.menuname);
                        $("#up_sort").val(res.data.sort);
                        $("#up_url").val(res.data.url);
                        $("#up_type").val(res.data.type);
                        $("#up_icon").val(res.data.icon);
                        $("#up_remark").val(res.data.remark);
                        //加载父菜单列表
                        $.ajax({
                            url : "menu/list/all",
                            type : "get",
                            success: function (resp) {
                                if(resp.status==200){
                                    var str = "<option value='0'>没有父菜单</option>";
                                    $.each(resp.data, function () {
                                        //在修改时，父菜单选项中看不到自己
                                        if(this.menuid!=res.data.menuid) {
                                            str += "<option value='" + this.menuid + "'>" + this.menuname + "</option>";
                                        }
                                    });
                                    $("#up_parent").html(str);
                                    $("#up_parent").val(res.data.parent.menuid);
                                    $("#update_win").modal("show");
                                }
                            }
                        });
                    }
                });
            }
        }
        //修改
        function doUpdate() {
            $.ajax({
                url : "menu/update",
                type : "post",
                data : {
                    menuid : $("#up_menuid").val(),
                    menuname : $("#up_menuname").val(),
                    sort : $("#up_sort").val(),
                    url : $("#up_url").val(),
                    type : $("#up_type").val(),
                    icon : $("#up_icon").val(),
                    "parent.menuid" : $("#up_parent").val(),
                    remark : $("#up_remark").val(),
                    updater : $("#up_updater").val()
                },
                success: function (res) {
                    if(res.status==200){
                        alert("修改成功");
                        $("#update_win").modal("hide");
                        $("#tab").bootstrapTable("refresh");
                        $("#update_form")[0].reset();
                    }
                }
            });
        }
    
        //删除
        function doDelete() {
            var rows = $("#tab").bootstrapTable("getSelections");
            if(rows.length==0){
                alert("请选择一条要删除的数据");
            }else{
                if(!confirm("确定要删除吗?")) return;
                var ids = [];
                $.each(rows, function () {
                    ids.push(this.menuid);
                });
                $.ajax({
                    url: "menu/removeBatch",
                    type: "get",
                    data: {
                        ids:ids
                    },
                    success: function (res) {
                        if(res.status==200){
                            alert("删除成功");
                            $("#tab").bootstrapTable("refresh");
                        }
                    }
                });
            }
        }
    </script>
</head>
<body>
    <!-- =================路径导航====================== -->
    <ol class="breadcrumb">
        <li>主页</li>
        <li>系统管理</li>
        <li class="active">菜单列表</li>
    </ol>
    <!-- =================路径导航.end====================== -->
    <!-- =================表格与操作按钮====================== -->
    <div class="panel-body">
        <form id="toolbar" class="form-inline">
            <div class="btn-group">
                <button type="button" class="btn btn-default" onclick="preInsert()">新增</button>
                <button type="button" class="btn btn-success" onclick="preUpdate()">修改</button>
                <button type="button" class="btn btn-danger" onclick="doDelete()">删除</button>
            </div>
            <div class="form-group">
                <input type="text" class="form-control" id="menuname" placeholder="菜单名称">
            </div>
            <div class="form-group">
                <select class="form-control" id="type">
                    <option value="">菜单类型</option>
                    <option value="M">目录</option>
                    <option value="C">菜单</option>
                    <option value="F">按钮</option>
                </select>
            </div>
            <div class="btn-group">
                <button type="button" class="btn btn-default" onclick="$('#tab').bootstrapTable('refresh');">搜索</button>
                <button type="button" class="btn btn-default" onclick="$('#toolbar')[0].reset();$('#tab').bootstrapTable('refresh');">清空</button>
            </div>
        </form>
        <table id="tab" class="table table-bordered table-hover table-striped"></table>
    </div>
    <!-- =================表格与操作按钮.end====================== -->
    <!-- =================新增窗体====================== -->
    <div class="modal fade" id="insert_win" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-sm" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">菜单新增</h4>
                </div>
                <div class="modal-body">
                    <form id="insert_form">
                        <input type="hidden" id="in_creator" value="${sessionScope.loginUser.username}"/>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">菜单名称</div>
                                <input type="text" class="form-control" id="in_menuname"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">显示顺序</div>
                                <input type="text" class="form-control" id="in_sort"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">请求地址</div>
                                <input type="text" class="form-control" id="in_url"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">菜单类型</div>
                                <select class="form-control" id="in_type">
                                    <option value="M">目录</option>
                                    <option value="C">菜单</option>
                                    <option value="F">按钮</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">菜单图标</div>
                                <input type="text" class="form-control" id="in_icon"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">父&ensp;菜&ensp;单</div>
                                <select class="form-control" id="in_parent">
                                    <option value="M">目录</option>
                                    <option value="C">菜单</option>
                                    <option value="F">按钮</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">备&emsp;&emsp;注</div>
                                <input type="text" class="form-control" id="in_remark"/>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="doInsert()">确定</button>
                </div>
            </div>
        </div>
    </div>
    <!-- =================新增窗体.end====================== -->
    <!-- =================修改窗体====================== -->
    <div class="modal fade" id="update_win" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-sm" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">菜单修改</h4>
                </div>
                <div class="modal-body">
                    <form id="update_form">
                        <input type="hidden" id="up_menuid">
                        <input type="hidden" id="up_updater" value="${sessionScope.loginUser.username}"/>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">菜单名称</div>
                                <input type="text" class="form-control" id="up_menuname"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">显示顺序</div>
                                <input type="text" class="form-control" id="up_sort"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">请求地址</div>
                                <input type="text" class="form-control" id="up_url"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">菜单类型</div>
                                <select class="form-control" id="up_type">
                                    <option value="M">目录</option>
                                    <option value="C">菜单</option>
                                    <option value="F">按钮</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">菜单图标</div>
                                <input type="text" class="form-control" id="up_icon"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">父&ensp;菜&ensp;单</div>
                                <select class="form-control" id="up_parent">
                                    <option value="1">目录</option>
                                    <option value="2">菜单</option>
                                    <option value="3">按钮</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">备&emsp;&emsp;注</div>
                                <input type="text" class="form-control" id="up_remark"/>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="doUpdate()">确定</button>
                </div>
            </div>
        </div>
    </div>
    <!-- =================修改窗体.end====================== -->
</body>
</html>
